<template>
  <div>
    <!-- style="color: #000000; margin-bottom: 14px;"-->


    <el-container
      style="height: 525px; overflow: hidden; background: white; border:1px solid white ; border-radius: 8px; margin-top: 10px;">
      <div style="margin-left: 4%;margin-top:10px;">
        <template>
          <el-table :data="tableData" style="width: 100%; height: 426px;  display: block; "
            :row-class-name="tableRowClassName">
            <!--
              private String proClass;
              	private String houseName;
              	private String oneArea;
              	private String twoArea;
              	private String threerea;
            -->
            <el-table-column prop="proClass" label="I级产品分类" width="200">
            </el-table-column>
            <el-table-column prop="houseName" label="库房名称" width="170">
            </el-table-column>
            <el-table-column prop="oneArea" label="存储区域名称" width="220">
            </el-table-column>
            <el-table-column label="分配">
              <template slot-scope="scope">
                <el-button v-if="scope.row.houseName != null" type="info" size="mini" @click="setKucun(scope.row)"
                  round>增加</el-button>
                <el-button v-if="scope.row.houseName == null" type="info" size="mini" @click="setKucun2(scope.row)"
                  round>分配</el-button>
              </template>
            </el-table-column>
            <el-table-column label="释放">
              <template slot-scope="scope">
                <el-button v-if="scope.row.houseName != null" type="danger" size="mini"
                  @click="handleDelete(scope.row)" round>释放</el-button>
              </template>

            </el-table-column>
          </el-table>
          <div class="block" style="display: block; margin-top: 15px;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
              :page-sizes="[1, 6, 7]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
          </div>
        </template>
      </div>
    </el-container>

    <el-dialog title="I级产品分类新增加" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="I级产品名称" :label-width="formLabelWidth">
          <el-input v-model="form.proName" autocomplete="off" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="请选择库房" :label-width="formLabelWidth">
          <el-select v-model="form.repid" placeholder="请选择" size="mini">
            <el-option v-for="i  in repHouse" :key="i.repid" :label="i.houseName" :value="i.repid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="存储区域名称" :label-width="formLabelWidth">
          <el-input v-model="form.areaName" autocomplete="off" size="mini"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="width: 45px ; height: 30px;" @click="dialogFormVisible = false">取 消</el-button>
        <el-button style="width: 45px ; height: 30px;" type="primary" @click="set()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="I级产品库存新分配" :visible.sync="dialogaddFormVisible">
      <el-form :model="form">
        <el-form-item label="I级产品名称" :label-width="formLabelWidth">
          <el-input v-model="form.proName" autocomplete="off" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="请选择库房" :label-width="formLabelWidth">
          <el-select v-model="form.repid" placeholder="请选择" size="mini">
            <el-option v-for="i  in repHouse" :key="i.repid" :label="i.houseName" :value="i.repid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="存储区域名称" :label-width="formLabelWidth">
          <el-input v-model="form.areaName" autocomplete="off" size="mini"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="width: 45px ; height: 30px;" @click="dialogaddFormVisible = false">取 消</el-button>
        <el-button style="width: 45px ; height: 30px;" type="primary" @click="updarep()">确 定</el-button>
      </div>
    </el-dialog>






  </div>
</template>

<script>
  export default {
    name: 'repi',
    data() {
      return {
        proName: '',
        repHouse: [],
        tableData: [],
        pageNum: 1,
        pageSize: 7,
        total: 0,
        dialogTableVisible: false,
        dialogFormVisible: false,
        dialogaddFormVisible: false,
        form: {
          pIid: 0,
          Iid: 0,
          repid: 0,
          proName: '',
          areaName: ''
        },
        formLabelWidth: '120px'
      }
    },
    methods: {
      tableRowClassName({
        row,
        rowIndex
      }) {
        if (rowIndex % 2 == 0) {
          return 'warning-row';
        } else {
          return 'success-row';
        }
        return '';
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.GetOnline(this.pageNum, this.pageSize);
      },
      handleCurrentChange(val) {
        this.pageNum = val;
        this.GetOnline(this.pageNum, this.pageSize);
      },
      GetOnline(pageNum, pageSize) {
        var data = new URLSearchParams();
        data.append("pageNum", pageNum);
        data.append("pageSize", pageSize);
        this.$http.post('rep/oneFindByPager', data).then(res => {
          this.pageNum = res.data.pageNum;
          this.pageSize = res.data.pageSize;
          this.total = res.data.total;
          this.tableData = res.data.rows;
        }).catch(err => {
          console.log(err);
        })
      },
      setKucun(kuId) {
        this.form.pIid = kuId.proId;
        this.$http.post('rep/getRepSet?kuId=' + kuId.kuId).then(res => {
          console.log(res);
          if (res.status == 200) {
            this.form.proName = res.data.proName;
            this.repHouse = res.data.repHouse;
            this.dialogFormVisible = true;
          }
        }).catch(err => {
          console.log(err);
        })
      },
      setKucun2(data) {
        this.form.Iid = data.kuId;
        this.$http.post('rep/getRepSet?kuId=' + data.kuId).then(res => {
          console.log(res);
          if (res.status == 200) {
            this.form.proName = res.data.proName;
            this.repHouse = res.data.repHouse;
            this.dialogaddFormVisible = true;
            this.GetOnline(this.pageNum, this.pageSize);
          }
        }).catch(err => {
          console.log(err);
        })
      },
      updarep() {
        var data = this.form;
        this.$http.post('rep/updateIKucun', this.$qs.stringify(data)).then(res => {
          console.log(res);
          if (res.status == 200) {
            this.$message({
              message: '更新成功',
              type: 'success'
            });
            this.GetOnline(this.pageNum, this.pageSize);
            this.dialogaddFormVisible = false;
          }
        }).catch(err => {
          console.log(err);
        })
      },
      set() {
        var data = this.form;
        this.$http.post('rep/insertNewKucun', this.$qs.stringify(data)).then(res => {
          console.log(res);
          if (res.data == 1) {
                  this.$message.error('该商品已设置此库存');
          }else{
            this.$message({
              message: '增加库存成功',
              type: 'success'
            });
            this.GetOnline(this.pageNum, this.pageSize);
            this.dialogFormVisible = false;
          }
        }).catch(err => {
          console.log(err);
        })
        //dialogFormVisible = false
      },
      handleDelete(datas) {
        var data = new URLSearchParams();
        data.append("pIid",datas.proId);
        data.append("Iid",datas.kuId);
        if (confirm('确定释放此库存?')) {
          this.$http.post('rep/setKCun',data).then(res => {
            console.log(res);
            if (res.status == 200) {
              this.$message({
                message: '释放库存成功',
                type: 'success'
              });
              this.GetOnline(this.pageNum, this.pageSize);
            }
          }).catch(err => {
            console.log(err);
          })
        }
      }
    },
    mounted() {
      this.GetOnline(this.pageNum, this.pageSize);
    }
  }
</script>

<style scoped="scoped">
  * {
    padding: 0px;
    margin: 0px;
  }

  .el-input {
    width: 340px;
  }

  .el-select {
    width: 340px;
  }

  .el-breadcrumb__inner {
    color: black;

  }

  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  .el-breadcrumb__item:last-child .el-breadcrumb__inner,
  .el-breadcrumb__item:last-child .el-breadcrumb__inner a,
  .el-breadcrumb__item:last-child .el-breadcrumb__inner a:hover,
  .el-breadcrumb__item:last-child .el-breadcrumb__inner:hover {
    font-weight: 400;
    color: black;
    cursor: text;
  }
</style>
